package net.cyue.homework.student.management.servlet.student;

import jakarta.servlet.annotation.WebServlet;
import jakarta.validation.Valid;
import net.cyue.homework.student.management.dto.DeleteDTO;
import net.cyue.homework.student.management.dto.JobAddDTO;
import net.cyue.homework.student.management.dto.JobUpdateDTO;
import net.cyue.homework.student.management.entity.Job;
import net.cyue.homework.student.management.http.HTTPServlet;
import net.cyue.homework.student.management.http.WebResult;
import net.cyue.homework.student.management.http.annotation.DeleteMethod;
import net.cyue.homework.student.management.http.annotation.PostMethod;
import net.cyue.homework.student.management.http.annotation.PutMethod;
import net.cyue.homework.student.management.util.DB;
import net.cyue.nest.nestjava.common.annotation.Body;
import net.cyue.nest.nestjava.common.annotation.Response;
import net.cyue.nest.nestjava.common.enums.HTTPStatus;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPRequest;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPResponse;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(
    name = "JobServlet",
    value = {
        "/api/job",
        "/api/job/"
    }
)
public class JobServlet extends HTTPServlet {

    @Override
    public void doGet(
        IHTTPRequest request,
        IHTTPResponse response
    ) throws IOException {
        String id = request.getParameter("id");
        if (id == null || id.isEmpty()) {
            response.code(HTTPStatus.BAD_REQUEST);
            response.send(WebResult.failed("id is required"));
            return;
        }
        try {
            ResultSet resultSet = DB.POOL.query("SELECT * FROM job WHERE id = " + id + " LIMIT 1");
            if (!resultSet.next()) {
                response.send(WebResult.failed("职务不存在"));
                return;
            }
            int jobID =resultSet.getInt("id");
            String name = resultSet.getString("name");
            response.send(WebResult.success("获取成功", new Job(jobID, name)));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，获取失败"));
        }
    }


    @PostMethod
    public void addJob(
        @Response IHTTPResponse response,
        @Valid @Body JobAddDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionPreparedUpdate(
            "INSERT INTO job (name) VALUES (?)",
                new Object[]{
                    dto.getName()
                }
            );
            if (rows == 0) {
                response.send(WebResult.failed("添加失败"));
                return;
            }
            response.send(WebResult.success("添加成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，添加失败"));
        }
    }


    @PutMethod
    public void updateJob(
        @Response IHTTPResponse response,
        @Valid @Body JobUpdateDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionPreparedUpdate(
            "UPDATE job SET name = ? WHERE id = ?",
                new Object[] {
                    dto.getName(),
                    dto.getId()
                }
            );
            if (rows == 0) {
                response.send(WebResult.failed("更新失败"));
                return;
            }
            response.send(WebResult.success("更新成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，更新失败"));
        }
    }

    @DeleteMethod
    public void deleteJob(
        @Response IHTTPResponse response,
        @Valid @Body DeleteDTO dto
    ) throws IOException {
        try {
            int rows = DB.POOL.transactionUpdate("DELETE FROM job WHERE id = " + dto.getId());
            if (rows == 0) {
                response.send(WebResult.failed("删除失败"));
                return;
            }
            response.send(WebResult.success("删除成功"));
        } catch (SQLException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
            response.send(WebResult.failed("内部错误，删除失败"));
        }
    }
}


